<?php
namespace app\index\controller;
use app\model\UserBill as UserBillModel;
use think\Db;


class Record extends Base
{   

    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $ulogin = $this->login_user;
        $record_type = input('record_type','streamlake');

        $map_params = [
           
        ];
        $map = getSearchParam($map_params);
        if ($ulogin['utype'] == 0) {
            $map[] = ['user_id','=',$ulogin['user_id']];
        }else{
            $map[] = ['ram_id','=',$ulogin['ram_id']];
        }

        if ($record_type == 'volcengine') {
            $result = $this->volcengine($map);
        }elseif ($record_type == 'klingai') {
            $result = $this->klingai($map);
        }else{
            $result = $this->streamlake($map);
        }

        $result['types'] = UserBillModel::getCollectList();

        
        return apiResult('2000','获取成功',$result);
    }

    public function nullChange($list){
        if (empty($list['data'])) {
            return $list;
        }
        foreach ($list['data'] as $k => $item) {
            foreach ($item as $i => $vo) {
                is_null($vo) && $item[$i] = '-';
            }
            $list['data'][$k] = $item;
        }
        return $list;
    }

    public function volcengine($map=[]){
        $ulogin = $this->login_user;

        $labels = [
            'tokens'=>'消耗TOKENS',
            'params'=>'请求参数',
            'api_result'=>'请求结果',

            'ctime'=>'请求时间',
            'utype'=>'账号类型',
            'user_id'=>'主账号',
            'ram_id'=>'子账号'
        ];

        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = Db::name('volcengine_record')->field(array_keys($labels))->where($map)->paginate($page_param)->toarray();

        $user_mobile = Db::name('user')->where('id',$ulogin['user_id'])->value('mobile');
        $rams = Db::name('ram')->field('id,account')->where('user_id',$ulogin['user_id'])->select();
        $rams = arrayParamExtract($rams,'id','account');

        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['ctime'] = timeDef($vo['ctime'],'','Y-m-d H:i:s');
            $list['data'][$k]['user_id'] = $user_mobile;
            $list['data'][$k]['ram_id'] = $rams[$vo['ram_id']];
        }

        $list = $this->nullChange($list);

        return ['labels'=>$labels,'data'=>$list];
    }

    public function streamlake($map=[]){
        $ulogin = $this->login_user;

        $labels = [
            'tokens'=>'消耗TOKENS',
            'params'=>'请求参数',
            'api_result'=>'请求结果',

            'ctime'=>'请求时间',
            'utype'=>'账号类型',
            'user_id'=>'主账号',
            'ram_id'=>'子账号'
        ];

        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = Db::name('streamlake_record')->field(array_keys($labels))->where($map)->paginate($page_param)->toarray();

        $user_mobile = Db::name('user')->where('id',$ulogin['user_id'])->value('mobile');
        $rams = Db::name('ram')->field('id,account')->where('user_id',$ulogin['user_id'])->select();
        $rams = arrayParamExtract($rams,'id','account');

        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['ctime'] = timeDef($vo['ctime'],'','Y-m-d H:i:s');
            $list['data'][$k]['user_id'] = $user_mobile;
            $list['data'][$k]['ram_id'] = $rams[$vo['ram_id']];
        }

        $list = $this->nullChange($list);

        return ['labels'=>$labels,'data'=>$list];
    }

    public function klingai($map=[]){
        $ulogin = $this->login_user;

        $labels = [
            'method'=>'请求方法',
            'path'=>'请求路径',
            'params'=>'请求参数',
            'api_result'=>'请求结果',
            'price'=>'费用',

            'ctime'=>'请求时间',
            'utype'=>'账号类型',
            'user_id'=>'主账号',
            'ram_id'=>'子账号'
        ];

        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = Db::name('klingai_record')->field(array_keys($labels))->where($map)->paginate($page_param)->toarray();

        $user_mobile = Db::name('user')->where('id',$ulogin['user_id'])->value('mobile');
        $rams = Db::name('ram')->field('id,account')->where('user_id',$ulogin['user_id'])->select();
        $rams = arrayParamExtract($rams,'id','account');

        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['ctime'] = timeDef($vo['ctime'],'','Y-m-d H:i:s');
            $list['data'][$k]['user_id'] = $user_mobile;
            $list['data'][$k]['ram_id'] = $rams[$vo['ram_id']];
        }

        $list = $this->nullChange($list);

        return ['labels'=>$labels,'data'=>$list];
    }

}
